import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    alias: '/login',
    component: () => import('../views/login/login.vue')
  },
  {
    path: '/home',
    redirect: '/home/first',
    component: () => import('../views/home/home.vue'),
    children: [
      {
        path: 'first',
        component: () => import('@/views/home/first/first.vue'),
        meta: {
          needTab: true
        }
      },
      {
        path: 'my',
        component: () => import('@/views/home/my/my.vue'),
        meta: {
          needTab: true
        }
      },
      {
        path: 'myInfo',
        component: () => import('@/views/home/my/myInfo.vue')
      },
      {
        path: 'search',
        component: () => import('@/views/home/search/search.vue'),
        meta: {
          needLogin: true,
          needTab: true
        }
      },
      {
        path: 'searchList',
        component: () =>
          import('@/views/home/search/search-list/search-list.vue'),
        meta: {
          needLogin: true
        }
      },
      {
        path: 'detail',
        component: () => import('@/views/home/search/detail/detail.vue'),
        meta: {
          needLogin: true
        }
      },
      {
        path: 'video',
        component: () => import('@/views/home/video/video.vue'),
        meta: {
          needTab: true
        }
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 这段代码不但能解决路由重复跳转的问题，还能解决路由的很多问题，一定要记得加上
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

router.beforeEach((to, from, next) => {
  // 这里还没有进行处理
  next()
})
// 后置守卫解决共用滚动条问题
router.afterEach((to, from) => {
  window.scrollTo(0, 0)
})

export default router
